using System; 
using System.Collections.Generic; 
using UnityEngine;
using Random = UnityEngine.Random;

public class CreateByCode : MonoBehaviour
{
    // Start is called before the first frame update


    void Start()
    {
        Debug.Log("-----------------CreateByCode-------------------");
        Debug.Log("这个脚本是通过代码AddComponent直接创建的");
        OpenCount();
        DemoTest();
    }

    // Update is called once per frame
    void Update()
    {
    }


    void DemoTest()
    {
        List<DemoData> dataList = new List<DemoData>();
        Dictionary<string, DemoData> dataDic = new Dictionary<string, DemoData>();
        
        DemoData data = null;
        int count = 5;
        for (int i = 0; i < 5; i++)
        {
            data = GetRandomDemoData(i);
        
            dataList.Add(data);
            dataDic.Add(data.name, data);
        
            Debug.Log($"_write__{i}__" + JsonUtility.ToJson(data));
        }
        
        Debug.Log($"----{dataList.Count} {dataDic.Count}");
        count = dataList.Count;
        for (int i = 0; i < count; i++)
        {
            Debug.Log("_Read__{i}__" + JsonUtility.ToJson(dataDic[dataList[i].name]));
        }
    }

    void OpenCount()
    {
        int count = PlayerPrefs.GetInt("OpenCount");
        count++;
        PlayerPrefs.SetInt("OpenCount", count);
        Debug.Log($"OpenCount:----------------{count}---------------------------");
    }


    DemoData GetRandomDemoData(int index)
    {
        DemoData data = new DemoData();
        data.name = $"asd_{index}";
        data.age = 10 + index;

        int r = Random.Range(0, 10);
        int rr = r * 10;

        data.Pos = new V3(r, r + 1, r + 2);
        data.Rot = new V3(rr, rr + 1, rr + 2);
        data.WayPonts.Add(data.Pos);
        data.WayPonts.Add(data.Rot);
        return data;
    }
}

[Serializable]
public class DemoData
{
    public string name;
    public int age;

    public V3 Pos = new V3();
    public V3 Rot = new V3();

    public List<V3> WayPonts = new List<V3>();
}

[Serializable]
public class V3
{
    public float X;
    public float Y;
    public float Z;

    public V3()
    {
    }

    public V3(float x, float y, float z)
    {
        this.X = x;
        this.Y = y;
        this.Z = z;
    }
}